package com.foxit.pdf2html5.web.controller;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.foxit.pdf2html5.web.common.Config;

@Controller
@Scope("prototype")
public class GetPageBinController{
	private static final Logger  log = Logger.getLogger(GetPageBinController.class);
	
	private String strPageIndex = null;
	
    private HttpServletRequest httpRequest;
    
    private HttpServletResponse httpResponse;
	
	private String docId = "";
	
	private String tempFilePath = "";
	
	private void setErrorResponse(int status, int errorCode) throws IOException
	{
		this.httpResponse.setHeader( "Pragma", "no-cache" );  
		this.httpResponse.setDateHeader("Expires", 0);        	  
		this.httpResponse.addHeader( "Cache-Control", "no-cache" );  
		this.httpResponse.addHeader( "Cache-Control", "no-store" );  
		this.httpResponse.addHeader( "Cache-Control", "must-revalidate" ); 
		Date now = new Date();
		this.httpResponse.addHeader("ETag",String.valueOf(now.getTime()));
		this.httpResponse.getWriter().print(String.valueOf(errorCode));
		this.httpResponse.setStatus(status);
	}
	
	@RequestMapping(value = "/GetPageBin.do")
	public void GetPageBin(HttpServletRequest request,HttpServletResponse response) throws IOException 
	{
		this.httpRequest = request;
		this.httpResponse = response;
		this.docId = this.httpRequest.getParameter("fileid");
		this.strPageIndex = this.httpRequest.getParameter("page");
		
		tempFilePath = this.httpRequest.getSession().getServletContext().getRealPath("temp");
		System.out.println(tempFilePath  + File.separator + this.docId);
		String tempPath = tempFilePath + File.separator + this.docId + File.separator + "docBin" + File.separator;
		String filePath = tempPath + this.strPageIndex + ".bin";
		File file = new File(filePath);
		if(file.exists()){
		DataInputStream dis=new DataInputStream(new FileInputStream(file));
		int size=dis.available();
		byte[] str = new byte[size]; 
		for(int i=0;i<size;i++){
			str[i] = (byte)dis.read();
		}
			this.writeImgStreamToClient(str);
		}else{
			this.writeImgStreamToClient("".getBytes());
		}
	}
	
	@RequestMapping(value = "/GetAnnotBin.do")
	public void GetAnnotBin(HttpServletRequest request,HttpServletResponse response) throws IOException 
	{
		this.httpRequest = request;
		this.httpResponse = response;
		
		tempFilePath = this.httpRequest.getSession().getServletContext().getRealPath("temp");
		String tempPath = tempFilePath + File.separator + this.docId + File.separator + "docBin" + File.separator;
		String filePath = tempPath + "a" + this.strPageIndex + ".bin";
		File file = new File(filePath);
		if(file.exists()){
			DataInputStream dis=new DataInputStream(new FileInputStream(file));
			int size = dis.available();
			byte[] str = new byte[size];
			for(int i=0;i<size;i++){
				str[i] = (byte)dis.read();
			}
		
			this.writeImgStreamToClient(str);
		}else{
			this.writeImgStreamToClient("".getBytes());
		}
	}
	
	 private void writeImgStreamToClient(byte[] binData) throws IOException
     {
        
		 this.httpResponse.setContentType("text/html");
		 Date now = new Date();  
     	 this.httpResponse.setDateHeader("Last-Modified",now.getTime());  
 		 this.httpResponse.setDateHeader("Expires", 0);        	  
		 this.httpResponse.addHeader( "Cache-Control", "no-cache" );  
		 this.httpResponse.addHeader( "Cache-Control", "must-revalidate" ); 
		 this.httpResponse.addHeader("ETag",String.valueOf(now.getTime()));
		 this.httpResponse.setContentLength(binData.length);
     	ServletOutputStream out = this.httpResponse.getOutputStream();
		out.write(binData);
		out.flush();
		out.close();
     }
}
